package continuum.service.resources;

import java.util.Date;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.SecurityContext;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import continuum.db.dao.Dao;
import continuum.db.exceptions.DaoException;
import continuum.db.schema.embeddables.Address;
import continuum.db.schema.embeddables.ContactInformation;
import continuum.db.schema.embeddables.SecurityDetails;
import continuum.db.schema.entities.Company;
import continuum.db.schema.entities.Employee;

@Component
@Scope("singleton")
@Path("/employee")
public class EmployeeEndpoint {

	private final Logger log = Logger.getLogger(getClass());

	@Resource
	private Dao<Employee, Integer> employeeDao;

	@Resource
	private Dao<Company, Integer> companyDao;

	@Context
	private SecurityContext securityContext;

	@GET
	@Transactional
	@Path("/{id: [0-9]+}")
	@Produces(MediaType.APPLICATION_JSON)
	@PreAuthorize("hasRole('ROLE_USER')")
	public Response getUser(@PathParam("id") final int id) {
		try {

			log.info("User Request Received: " + id + " " + employeeDao);
			Employee user = employeeDao.select(id);
			if (user == null) {
				return Response.status(Status.NOT_FOUND).build();
			} else {
				user.getAuthorizedTasks();
				return Response.ok(user).build();
			}
		} catch (DaoException e) {
			e.printStackTrace();
			return Response.status(Status.INTERNAL_SERVER_ERROR).build();
		}

	}

	@POST
	@Transactional
	@Path("/{id : [0-9]+}")
	@PreAuthorize("hasRole('ROLE_USER')")
	public Response createUser(@PathParam("id") final int id) {
		try {
			Company company = new Company();
			company.setCompany("inovex");
			company.setId(1);
			company.setDisplayName("Inovex Information Systems");
			companyDao.insert(company);

			// Company company = companyDao.select(1);

			Employee employee = new Employee();
			ContactInformation ci = new ContactInformation();
			ci.setEmailAddress("test@test.com");
			ci.setFirstName("test");
			ci.setLastName("test");
			ci.setPhoneNumber("123-123-1234");
			Address add = new Address();
			add.setApartmentPoBox("test");
			add.setCity("test");
			add.setState("md");
			add.setStreetAddress("123 123 123");
			add.setZipCode(21044);
			ci.setAddress(add);
			employee.setContactInformation(ci);
			SecurityDetails sd = new SecurityDetails();
			sd.setLastLogin(new Date());
			sd.setPassword("password");
			sd.setUsername("testuser");
			employee.setSecurityDetails(sd);
			employee.setCompany(company);
			employeeDao.insert(employee);
			return Response.ok().build();
		} catch (DaoException e) {
			e.printStackTrace();
			return Response.status(Status.INTERNAL_SERVER_ERROR).build();
		}
	}

}
